if (!QS) {
    var QS = {};
}

QS.InterestRatesDataManager=function(url){
    if (typeof(QS_PROTOCOL) == 'undefined') {
        QS_PROTOCOL = 'http://';
    }
    //this.QS_InterestRates_Data_Path = QS_PROTOCOL + QS_SERVER_DOMAIN + 'core/obtainLastLiborData';
	this.QS_InterestRates_Data_Path = url;
};

QS.InterestRatesDataManager.prototype={
    getData:function(callback){
        $.get(this.QS_InterestRates_Data_Path,callback);                
    }
};

QS.InterestRates=function(container,cfg,callback){
    this.container=container;
    this.data={};
    this.tabData=[];
    this.config={
        'showTab':true,
        'topOffset':57,
        'leftOffset':40
    };
    this.callback=callback;
    this.tabmenu = null;
    this.barChart = null;
    $.extend(true, this.config, cfg);
    this.init();
};

QS.InterestRates.prototype={
    init:function(){
        var self = this;
		this.dataManager=new QS.InterestRatesDataManager(this.config.dataURL);
        this.dataManager.getData(function(data){
			if(!data){
                return;
            }else{
                self.data = $.evalJSON(data); 
				if(self.config.showTab){
		            for(var o in self.data){
						self.tabData.push({'txt':o,'id':o,'closable':false});
		            }
		            self._createTabMenu();
		        }
		        if(self.data['Libor'].length>0){
		            self._createBarChart(self.data['Libor']);
		        }               
            }			                       
        });
    },
    _createTabMenu:function(){
        var self = this;        
        this.tabmenu = new Util.TabMenu(this.container, self.tabData, {
            onActive: function(tab){
                var index = tab.txt;
                if(self.barChart){
                    self._switchBarChart(self.data[index]);                           
                }                       
            }
        },{
            width:65,
            autoResize:true,
            autoFixtoText:false
        });
        this.tabmenu.menusWrap.css({'paddingLeft':this.config.leftOffset});
    },
    _createBarChart:function(data){
        var width = parseInt(this.config.width,10)||$('body').width();
        var height = parseInt(this.config.height,10)||$('body').height();
        width = width-this.config.leftOffset-10;
        height = height-this.config.topOffset-10;
        this.barChart = new Util.barChart(this.container,data,{'width':width,'height':height},{});
    },
    _switchBarChart:function(data){
        this.barChart.reBindData(data);
    },
    resize:function(w,h){
        var width = parseInt(w,10)||$('body').width();
        var height = parseInt(h,10)||$('body').height();
        width = width-this.config.leftOffset-10;
        height = height-this.config.topOffset-10;
        if(this.barChart){
            this.barChart.resize(width,height);
        }        
    },
    clear:function(){
        for(var p in this){
            if(!p || p == 'clear'){continue;}
            this[p] = null;
            delete this[p];
        }
    }
};